//
//  HNBankListController.m
//  XDEducation
//
//  Created by 星道 on 17/5/22.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNBankListController.h"
#import "HNBankListSegment.h"
#import "HNBankListModel.h"
#import "HNBankListCell.h"
@interface HNBankListController ()<UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UIView *_SegmentView;
    IBOutlet UITableView *_TableView;
}
@property (nonatomic, strong) HNBankListSegment *Segment;
@property (nonatomic, strong) NSArray *TimeArr;
@property (nonatomic, assign) NSInteger TimeIndex;
@property (nonatomic, assign) NSInteger Index;
@property (nonatomic, strong) NSMutableArray *DataArr;
@property (nonatomic, assign) NSInteger PageIndex;
@end

@implementation HNBankListController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.PageIndex = 1;
    
    //选择控件
    [_SegmentView addSubview:self.Segment];
    self.Segment.frame = _SegmentView.frame;
    
    //配置表单
    [self ConfigTableView];
}



#pragma mark - Config
- (void)ConfigTableView
{
    _TableView.tableFooterView = [UIView new];
    _TableView.tableHeaderView = [UIView new];
    
    [_TableView registerNib:[UINib nibWithNibName:[HNBankListCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    
    _TableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self RefreshDataWithTimeIndex:self.TimeIndex FruitType:self.Index];
    }];
}


#pragma mark - 网络请求
- (void)RefreshDataWithTimeIndex:(NSInteger)TimeIndex FruitType:(NSInteger)FruitType
{
    NSDictionary *Dict = @{
                           @"uid"       :[AccountTool shareAccountTool].currentAccount.uid,
                           @"page"      :@(self.PageIndex),
                           @"fruitType" :@(FruitType),
                           @"role"      :@4,
                           @"dateType"  :self.TimeArr[TimeIndex]
                           };
    [MBProgressHUD showHUD];
    [XDHttpTool get:BaseURL(@"user.do?action=getMyFruitHistoryByTime") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Dict success:^(id responseObj) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [_TableView.mj_footer endRefreshing];
        });
        if (responseObj)
        {
            if ([responseObj count] > 0)
            {
                for (NSDictionary *Dict in responseObj)
                {
                    HNBankListModel *Model = [[HNBankListModel alloc] init_HNObjectKey:Dict];
                    [self.DataArr addObject:Model];
                }
                [_TableView reloadData];
                self.PageIndex += 1;
            }
            else
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_TableView.mj_footer endRefreshingWithNoMoreData];
                });
            }
        }
        else
        {
            [MBProgressHUD showError:@"服务器链接失败"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_TableView.mj_footer endRefreshing];
        });
    }];
}


#pragma mark - Action
//选择时间
- (IBAction)SelectTimer:(UIButton *)sender
{
    UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"" message:@"请选择时间" preferredStyle:(UIAlertControllerStyleActionSheet)];
    [Alert addAction:[UIAlertAction actionWithTitle:@"一周之内" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [self MgrAlertWithTitle:@"一周之内" TimeIndex:0 WeakSender:sender];
    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"一月之内" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [self MgrAlertWithTitle:@"一月之内" TimeIndex:1 WeakSender:sender];

    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"一年之内" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [self MgrAlertWithTitle:@"一年之内" TimeIndex:2 WeakSender:sender];

    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDestructive ) handler:nil]];
    [self presentViewController:Alert animated:YES completion:nil];
}

//Alert数据处理
- (void)MgrAlertWithTitle:(NSString *)Title TimeIndex:(NSInteger)TimeIndex WeakSender:(UIButton *)WeakSender
{
    [self.DataArr removeAllObjects];
    self.TimeIndex = TimeIndex;
    self.PageIndex = 1;
    dispatch_async(dispatch_get_main_queue(), ^{
        [WeakSender setTitle:Title forState:(UIControlStateNormal)];
        [_TableView reloadData];
    });
    [self RefreshDataWithTimeIndex:self.TimeIndex FruitType:self.Index];
}

#pragma mark - tableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.DataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNBankListCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    HNBankListModel *Model = self.DataArr[indexPath.row];
    Cell.Model = Model;
    return Cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

#pragma mark - Setter
- (HNBankListSegment *)Segment
{
    if (!_Segment)
    {
        _Segment = [[HNBankListSegment alloc] init_HNWithSelectIndex:^(NSInteger Index, NSString *Str) {
            
            [self.DataArr removeAllObjects];
            self.Index = Index+1;
            self.PageIndex = 1;
            dispatch_async(dispatch_get_main_queue(), ^{
                [_TableView reloadData];
            });
            //发起请求
            [self RefreshDataWithTimeIndex:self.TimeIndex FruitType:self.Index];
        }];
    }
    return _Segment;
}

- (NSArray *)TimeArr
{
    if (!_TimeArr)
    {
        _TimeArr = @[@"1",@"2",@"3"];
    }
    return _TimeArr;
}

- (NSMutableArray *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}
@end
